package red.util;

import com.sun.javafx.logging.Logger;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MysqlConnectUtils {

    private Statement state = null;
    public static Connection conn = null;

    public Logger log = null;

    public MysqlConnectUtils() {

    }


    public void connection() {
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://10.1.2.10:3306/water_db?useUnicode=true&characterEncoding=UTF-8";
            String userName = "mmzy";
            String password = "123456";
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("数据库连接成功!");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            state.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public List<Map<String, Object>> list(String sql) {
        this.connection();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            while (rs.next()) {
                Map<String, Object> rowData = new HashMap<String, Object>();
                for (int i = 1; i <= count; i++) {
                    rowData.put(rsmd.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();
        return list;
    }

    public boolean insert(String sql) {
        try {
            this.connection();
            state = conn.createStatement();
            state.execute(sql);
            close();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            close();
            return false;
        }
    }

    public boolean delete(String sql) {
        try {
            this.connection();
            state = conn.createStatement();
            state.executeUpdate(sql);
            close();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            close();
            return false;
        }
    }

    public boolean update(String sql) {
        try {
            this.connection();
            state = conn.createStatement();
            state.executeUpdate(sql);
            close();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            close();
            return false;
        }
    }

}

